package cn.jja8.config.tool.work.obj;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public interface ObjKeep<V> {
    default Class<V> Type() {
        Type[] types = this.getClass().getGenericInterfaces();
        Type type = null;
        for (Type typeT : types) {
            if ( typeT instanceof ParameterizedType){
                if (ObjKeep.class.isAssignableFrom((Class<?>) ((ParameterizedType) typeT).getRawType())) {
                    type = typeT;
                }
            }
        }
        if (type==null){
            throw new Error(this.getClass()+"没有泛型参数也没有重写Type方法!");
        }
        Type[] classss = ((ParameterizedType) type).getActualTypeArguments();
        if (classss.length!=1){
            throw new Error(this.getClass()+"的泛型数量错误！");
        }
        if (!(classss[0] instanceof Class)){
            throw new Error(this.getClass()+"的泛型不能使用通配符泛型！");
        }
        return (Class<V>) classss[0];
    }
}
